package _interview150;

import _object.TreeNode;

/**
 * 230. 二叉搜索树中第K小的元素
 */
public class No230 {
    private int ans, k;

    public int kthSmallest(TreeNode root, int k) {
        this.k = k;
        dfs(root);
        return ans;
    }

    private void dfs(TreeNode node) {
        if (node == null) return;
        dfs(node.left);
        if (--k == 0) {
            ans = node.val;
            return;
        }
        dfs(node.right);
    }
}
